package conf

import (
	"gopkg.in/ini.v1"
)

type conf struct {
	App      app
	Database database
	Jwt      jwt
	Redis    redis
	Server   server
	User     user
	File     file
	Mongodb  mongodb
}

const iniPath = "config.ini"

var All conf
var cfg *ini.File

func init() {
	var err error
	cfg, err = ini.Load(iniPath)
	if err != nil {
		panic(err)
	}
	c := conf{
		App:      App,
		Database: Database,
		Jwt:      Jwt,
		Redis:    Redis,
		Server:   Server,
		User:     User,
		File:     File,
		Mongodb:  Mongodb,
	}
	err = cfg.MapTo(&c)
	if err != nil {
		panic(err)
	}
	All = c
	App = c.App
	Database = c.Database
	Jwt = c.Jwt
	Redis = c.Redis
	Server = c.Server
	User = c.User
	File = c.File
	Mongodb = c.Mongodb
}

func save() error {
	return cfg.SaveTo(iniPath)
}
